
library(lme4)
library(dplyr)

#load data file: denver_note-results.RData

#Neighborhood Effects====

#Vio Ped

# Summary of the model
summary(t1p2_pstop)

# Calculate total effects for each neighborhood
vio_ped_coef <- as.data.frame(t(apply(ranef(t1p2_pstop)$neighborhood,
                                      1,function(x) fixef(t1p2_pstop) + x)))

# Create a tibble with neighborhood names and total effects
index<-vio_ped_coef
index <- tibble::rownames_to_column(index, "Neighborhood")
index<-index[,c(1,5)]
names(index)<-c("Neighborhood","Coefficient")
index$Model<-"Violent_Ped_Total_Effect"

# Extract random effects for the term "pstop_wgt_avg_3wk_dev"
vio_ped_ranef <- as.data.frame(ranef(t1p2_pstop))
vio_ped_ranef<-vio_ped_ranef %>%
  filter(term=="pstop_wgt_avg_3wk_dev") %>%
  select(grp,condval)
names(vio_ped_ranef)<-c("Neighborhood","Coefficient")
vio_ped_ranef$Model<-"Violent_Ped_Random_Effect"

# Combine total effects and random effects into one data frame
index<-rbind(index,vio_ped_ranef)

# Verify the final data frame
print(index)


#Prop Ped

# Summary of the model
summary(t1p3_pstop)

# Calculate total effects for each neighborhood
# 'apply' is used to add the fixed effects (fixef) to the random effects (ranef)
prop_ped_coef <- as.data.frame(t(apply(ranef(t1p3_pstop)$neighborhood,
                                         1,function(x) fixef(t1p3_pstop) + x)))

# Create a tibble with neighborhood names and total effects
# Convert row names (neighborhoods) to a column
prop_ped_coef <- tibble::rownames_to_column(prop_ped_coef, "Neighborhood")
# Select relevant columns (ensure the correct column index for total effects)
prop_ped_coef<-prop_ped_coef[,c(1,5)]

# Select relevant columns (ensure the correct column index for total effects)
names(prop_ped_coef)<-c("Neighborhood","Coefficient")

# Add a new column to indicate the model type
prop_ped_coef$Model<-"Property_Ped_Total_Effect"

# Combine the total effects with the existing data frame
index<-rbind(index,prop_ped_coef)

# Extract random effects for the term "pstop_wgt_avg_3wk_dev"
prop_ped_ranef <- as.data.frame(ranef(t1p3_pstop))
prop_ped_ranef<-prop_ped_ranef %>%
  filter(term=="pstop_wgt_avg_3wk_dev") %>%
  select(grp,condval)

# Rename the columns for clarity
names(prop_ped_ranef)<-c("Neighborhood","Coefficient")
# Add a new column to indicate the model type
prop_ped_ranef$Model<-"Property_Ped_Random_Effect"

# Combine the random effects with the existing data frame
index<-rbind(index,prop_ped_ranef)


# Verify the final data frame
print(index)



#Prop Drugs

# Summary of the model
summary(t1p3_darrest)

# Calculate total effects for each neighborhood
# 'apply' is used to add the fixed effects (fixef) to the random effects (ranef)
prop_drug_coef <- as.data.frame(t(apply(ranef(t1p3_darrest)$neighborhood,
                                       1,function(x) fixef(t1p3_darrest) + x)))

# Create a tibble with neighborhood names and total effects
# Convert row names (neighborhoods) to a column
prop_drug_coef <- tibble::rownames_to_column(prop_drug_coef, "Neighborhood")
# Select relevant columns (ensure the correct column index for total effects)
prop_drug_coef<-prop_drug_coef[,c(1,5)]

# Select relevant columns (ensure the correct column index for total effects)
names(prop_drug_coef)<-c("Neighborhood","Coefficient")

# Add a new column to indicate the model type
prop_drug_coef$Model<-"Property_Drug_Total_Effect"

# Combine the total effects with the existing data frame
index<-rbind(index,prop_drug_coef)

# Extract random effects for the term "arrest_drug_wgt_avg_3wk_dev"
prop_drug_ranef <- as.data.frame(ranef(t1p3_darrest))
prop_drug_ranef<-prop_drug_ranef %>%
  filter(term=="arrest_drug_wgt_avg_3wk_dev") %>%
  select(grp,condval)

# Rename the columns for clarity
names(prop_drug_ranef)<-c("Neighborhood","Coefficient")
# Add a new column to indicate the model type
prop_drug_ranef$Model<-"Property_Drug_Random_Effect"

# Combine the random effects with the existing data frame
index<-rbind(index,prop_drug_ranef)


# Verify the final data frame
print(index)




# verify neighborhoods align with numerical indicators
unique(data$neighborhood_key)
# Define the vector mapping neighborhood codes to names
neighborhood_names <- c("Athmar Park", "Auraria", 
                        "Baker", "Barnum", 
                        "Barnum West", "Bear Valley", 
                        "Belcaro", "Berkeley", 
                        "CBD", "Capitol Hill", 
                        "Central Park", "Chaffee Park", 
                        "Cheesman Park", "Cherry Creek",
                        "City Park", "City Park West", 
                        "Civic Center", "Clayton",
                        "Cole", "College View/South Platte", 
                        "Congress Park", "Cory-Merrill", 
                        "Country Club", "DIA", 
                        "East Colfax", "Elyria Swansea", 
                        "Five Points", "Fort Logan", 
                        "Gateway-Green Valley Ranch", "Globeville", 
                        "Goldsmith", "Hale", 
                        "Hampden", "Hampden South",
                        "Harvey Park", "Harvey Park South",
                        "Highland", "Hilltop",
                        "Indian Creek", "Jefferson Park", 
                        "Kennedy", "Lincoln Park",
                        "Lowry Field", "Mar Lee", 
                        "Marston", "Montbello", 
                        "Montclair", "North Capitol hill", 
                        "North Park Hill", "Northeast Park Hill",
                        "Overland", "Platt Park", 
                        "Regis", "Rosedale", 
                        "Ruby Hill","Skyland", 
                        "Sloan Lake", "South Park Hill", 
                        "Southmoor Park","Speer", 
                        "Sun Valley", "Sunnyside", 
                        "Union Station","University", 
                        "University Hills", "University Park", 
                        "Valverde","Villa Park", 
                        "Virginia Village", "Washington Park",
                        "Washington Park West", "Washington Virginia Vale", 
                        "Wellshire", "West Colfax", 
                        "West Highland", "Westwood", 
                        "Whittier", "Windsor")

# Assign names to the vector
names(neighborhood_names) <- as.character(1:78)

#recheck neighborhood name alignment
neighborhood_names

# Convert the 'Neighborhood' column to character if it is not already
index$Neighborhood <- as.character(index$Neighborhood)

# Map the neighborhood names using the vector
index$Neighborhood_Name <- neighborhood_names[index$Neighborhood]

# Check the updated dataframe
head(index)



# write file====

#write.csv(index, "")





